package Utils::Digital; {

use strict;
use warnings;
use utf8;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); 

$VERSION = '0.05';

# Preloaded methods go here.
use vars qw(%diw %nom); 

%diw = (
    0 => {
        0 => { 0 => "ноль", 1 => 1},
        1 => { 0 => "", 1 => 2},
        2 => { 0 => "", 1 => 3},
        3 => { 0 => "три", 1 => 0},
        4 => { 0 => "четыре", 1 => 0},
        5 => { 0 => "пять", 1 => 1},
        6 => { 0 => "шесть", 1 => 1},
        7 => { 0 => "семь", 1 => 1},
        8 => { 0 => "восемь", 1 => 1},
        9 => { 0 => "девять", 1 => 1},
        10 => { 0 => "десять", 1 => 1},
        11 => { 0 => "одиннадцать", 1 => 1},
        12 => { 0 => "двенадцать", 1 => 1},
        13 => { 0 => "тринадцать", 1 => 1},
        14 => { 0 => "четырнадцать", 1 => 1},
        15 => { 0 => "пятнадцать", 1 => 1},
        16 => { 0 => "шестнадцать", 1 => 1},
        17 => { 0 => "семнадцать", 1 => 1},
        18 => { 0 => "восемнадцать", 1 => 1},
        19 => { 0 => "девятнадцать", 1 => 1},
    },
    1 => {
        2 => { 0 => "двадцать", 1 => 1},
        3 => { 0 => "тридцать", 1 => 1},
        4 => { 0 => "сорок", 1 => 1},
        5 => { 0 => "пятьдесят", 1 => 1},
        6 => { 0 => "шестьдесят", 1 => 1},
        7 => { 0 => "семьдесят", 1 => 1},
        8 => { 0 => "восемьдесят", 1 => 1},
        9 => { 0 => "девяносто", 1 => 1},
    },
    2 => {
        1 => { 0 => "сто", 1 => 1},
        2 => { 0 => "двести", 1 => 1},
        3 => { 0 => "триста", 1 => 1},
        4 => { 0 => "четыреста", 1 => 1},
        5 => { 0 => "пятьсот", 1 => 1},
        6 => { 0 => "шестьсот", 1 => 1},
        7 => { 0 => "семьсот", 1 => 1},
        8 => { 0 => "восемьсот", 1 => 1},
        9 => { 0 => "девятьсот", 1 => 1}
    }
); 

%nom = (
#    0 => {0 => "тийин", 1 => "тийин", 2 => "один тийин", 3 => "две тийин"},
#    1 => {0 => "сум", 1 => "сум", 2 => "один сум", 3 => "два сума"},
    0 => {0 => "", 1 => "", 2 => "один", 3 => "две"},
    1 => {0 => "сум", 1 => "", 2 => "один", 3 => "два"},
    2 => {0 => "тысячи", 1 => "тысяч", 2 => "одна тысяча", 3 => "две тысячи"},
    3 => {0 => "миллиона", 1 => "миллионов", 2 => "один миллион", 3 => "два миллиона"},
    4 => {0 => "миллиарда",1 => "миллиардов",2 => "один миллиард",3 => "два миллиарда"},
    5 => {0 => "триллиона",1 => "триллионов",2 => "один триллион",3 => "два триллиона"} ); 

my $out_rub; 
sub rur_in_words {
    my ($sum) = shift;
    my ($retval, $i, $sum_rub, $sum_kop);
    $retval = "";
    $out_rub = ($sum >= 1) ? 0 : 1;
    $sum_rub = sprintf("%0.0f", $sum);
    $sum_rub-- if (($sum_rub - $sum) > 0);
    $sum_kop = sprintf("%0.2f",($sum - $sum_rub))*100;
    my $kop = get_string($sum_kop, 0);
    for ($i=1; $i<6 && $sum_rub >= 1; $i++) {
        my $sum_tmp = $sum_rub/1000;
        my $sum_part = sprintf("%0.3f", $sum_tmp - int($sum_tmp))*1000;
        $sum_rub = sprintf("%0.0f",$sum_tmp);
        $sum_rub-- if ($sum_rub - $sum_tmp > 0);
        $retval = get_string($sum_part, $i)." ".$retval;
    }
    $retval .= " сумов" if ($out_rub == 0);
    $retval .= " ".$kop if $kop;
    $retval =~ s/\s+/ /g;
    return $retval;
};

sub get_string {
    my ($sum, $nominal) = @_;
    return('') if !$sum;
    my ($retval, $nom) = ('', -1);
    if (($nominal == 0 && $sum < 100) || ($nominal > 0 && $nominal < 6 && $sum < 1000)) {
        my $s2 = int($sum/100);
        if ($s2 > 0) {
            $retval .= " ".$diw{2}{$s2}{0};
            $nom = $diw{2}{$s2}{1};
        }
        my $sx = sprintf("%0.0f", $sum - $s2*100);
        $sx-- if ($sx - ($sum - $s2*100) > 0);
        if (($sx<20 && $sx>0) || ($sx == 0 && $nominal == 0)) {
            $retval .= " ".$diw{0}{$sx}{0};
            $nom = $diw{0}{$sx}{1};
        } else {
            my $s1 = sprintf("%0.0f",$sx/10);
            $s1-- if (($s1 - $sx/10) > 0);
            my $s0 = int($sum - $s2*100 - $s1*10 + 0.5);
            if ($s1 > 0) {
                $retval .= " ".$diw{1}{$s1}{0};
                $nom = $diw{1}{$s1}{1};
            }
            if ($s0 > 0) {
                $retval .= " ".$diw{0}{$s0}{0};
                $nom = $diw{0}{$s0}{1};
            }
        }
    }
    if ($nom >= 0) {
        $retval .= " ".$nom{$nominal}{$nom};
        $out_rub = 1 if ($nominal == 1);
    }
    $retval =~ s/^\s*//g;
    $retval =~ s/\s*$//g;
    return $retval;
};

# Autoload methods go after =cut, and are processed by the autosplit program.
1;

};

__END__
